<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>题目管理</el-breadcrumb-item>
      <el-breadcrumb-item>题目列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!--绘制题目列表卡片-->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <!--添加题目组件-->
        <el-col :span="4">
          <el-button type="primary" @click="addProblemDialogVisible = true"
            >添加题目</el-button
          >
        </el-col>
      </el-row>

      <!-- 卡片内数据表格内容 -->
      <el-table :data="problemTableData" border style="width: 100%">
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column
          prop="problemName"
          label="题目名称"
          width="180"
        ></el-table-column>

        <el-table-column prop="problemDesc" label="题目描述"></el-table-column>
        <el-table-column prop="timeLimit" label="时间限制">
          <template v-slot="scope">
            <el-tag type="success"> {{ scope.row.timeLimit }} MS </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="spaceLimit" label="内存限制">
          <template v-slot="scope">
            <el-tag type="success"> {{ scope.row.spaceLimit }} KB </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <!--设置 修改、删除、设置 按钮-->
          <template v-slot="scope">
            <!-- 修改 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showUpdateProblemDialog(scope.row.problemId)"
            ></el-button>

            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeProblemById(scope.row.problemId)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页插件-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageParam.pageNo"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageParam.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 修改题目对话框 -->
      <el-dialog
        title="修改题目"
        :visible.sync="updateProblemDialogVisible"
        width="50%"
        @close="updateProblemDialogClosed"
      >
        <el-form
          :model="editProblemForm"
          :rules="editProblemFormRules"
          ref="editProblemFormRef"
          label-width="120px"
        >
         <el-form-item label="题目Id:" prop="problemId" >
            <el-input disabled v-model="editProblemForm.problemId"></el-input>
          </el-form-item>

          <el-form-item label="题目名称:" prop="problemName">
            <el-input v-model="editProblemForm.problemName"></el-input>
          </el-form-item>

          <el-form-item label="题目描述:" prop="problemDesc">
            <el-input
              :rows="3"
              type="textarea"
              v-model="editProblemForm.problemDesc"
            ></el-input>
          </el-form-item>

          <el-form-item label="时间限制(MS):" prop="timeLimit">
            <el-input v-model="editProblemForm.timeLimit"></el-input>
          </el-form-item>

          <el-form-item label="内存限制(KB)" prop="spaceLimit">
            <el-input v-model="editProblemForm.spaceLimit"></el-input>
          </el-form-item>

          <el-form-item label="题目输入:" prop="problemInput">
            <el-input
              :rows="2"
              type="textarea"
              v-model="editProblemForm.problemInput"
            ></el-input>
          </el-form-item>

          <el-form-item label="题目输出:" prop="problemOut">
            <el-input
              :rows="2"
              type="textarea"
              v-model="editProblemForm.problemOut"
            ></el-input>
          </el-form-item>
          <el-form-item label="样例输入:" prop="sampleInput">
            <el-input
              :rows="2"
              type="textarea"
              v-model="editProblemForm.sampleInput"
            ></el-input>
          </el-form-item>
          <el-form-item label="样例输出:" prop="sampleOutput">
            <el-input
              :rows="2"
              type="textarea"
              v-model="editProblemForm.sampleOutput"
            ></el-input>
          </el-form-item>
        </el-form>

        <!-- 修改用户提示框底部 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="updateProblemDialogVisible = false"
            >取 消</el-button
          >
          <el-button type="primary" @click="editProblem">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 分页数据
      pageParam: {
        pageSize: 5,
        pageNo: 1,
        param: {}
      },
      // 总共条数
      total: 0,

      problemTableData: [],

      // 修改显示框的显示
      // 提示框显示状态
      updateProblemDialogVisible: false,



      // 修改题目对话框数据
      editProblemForm: {},

      // 修改题目字段规则
      editProblemFormRules: {
        problemName: [
          { required: true, message: "请输入题目名称", trigger: "blur" },
        ],

        problemDesc: [
          { required: true, message: "请输入题目描述", trigger: "blur" },
        ],

        timeLimit: [
          { required: true, message: "请输入时间限制", trigger: "blur" },
        ],

        spaceLimit: [
          { required: true, message: "请输入内存限制", trigger: "blur" },
        ],

        problemInput: [
          { required: true, message: "请输入题目输入", trigger: "blur" },
        ],
        problemOut: [
          { required: true, message: "请输入题目输出", trigger: "blur" },
        ],
        sampleInput : [
          { required: true, message: "请输入样例输入", trigger: "blur" },
        ],
        sampleOutput: [
          { required: true, message: "请输入样例输出", trigger: "blur" },
        ],

      }
    };
  },
  methods: {
    handleSizeChange(newSize) {
      this.pageParam.pageSize = newSize;
      this.getProblemList();
    },
    handleCurrentChange(newNum) {
      this.pageParam.pageNo = newNum;
      this.getProblemList();
    },
    // 获取题目排名分页数据
    async getProblemList() {
      const { data: res } = await this.$http.post(
        "problem/page",
        this.pageParam
      );

      if (res.meta.code !== 200) {
        return this.$message.error("获取题目排名失败");
      }

      this.problemTableData = res.data.records;
      this.total = res.data.total;
      console.log(res);
    },
    // 提交表单修改题目
    editProblem() {

       this.$refs.editProblemFormRef.validate(async valid => {
        if (!valid) {
          return;
        }

        console.log(this.editProblemForm);
        // 发起修改用户信息的请求

        const { data: res } = await this.$http.put("problem/update", this.editProblemForm);

        if (res.meta.code !== 200) {
          return this.$message.error("更新用户信息失败!");
        }

        this.updateProblemDialogVisible = false;

        this.$message.success("更新题目信息成功!");
        this.getProblemList();
      });
    },

    // 设置显示修改对话框
    //显示更改用户信息 Dialog
    async showUpdateProblemDialog(id) {
      this.updateProblemDialogVisible = true;

      const { data: res } = await this.$http.get("problem/get-by-id/" + id);

      console.log(res);
      this.editProblemForm = res.data;
    },

    // 修改题目关闭事件
    updateProblemDialogClosed() {
      this.$refs.editProblemFormRef.resetFields();
    },

    // 删除题目
    // 删除用户信息
    removeUserById(id) {
      console.log(id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {

          const { data: res } = await this.$http.delete("problem/delete-by-id/" + id);

          if (res.meta.code !== 200) {
            return this.$message.error("删除失败");
          }

          //点击确认删除触发的事件
          this.$message({
            type: "success",
            message: "删除成功!"
          });

          // 重新获取用户列表
          this.getAdminList();
        })
        .catch(() => {
          // 点击取消触发的事件
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
  },
  created() {
    //获取问题列表

    this.getProblemList();
  }
};
</script>

<style scoped>
a {
  text-decoration: none;
}

.router-link-active {
  text-decoration: none;
}
</style>
